package com.itbaizhan.common.redis.dao.impl;

import com.itbaizhan.common.redis.dao.RedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

@Repository
public class RedisDaoImpl implements RedisDao {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public Long incr(String key) {
        return redisTemplate.opsForValue().increment(key, 1);
    }

    @Override
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void set(String key, Object value, long times, TimeUnit unit) {
        redisTemplate.opsForValue().set(key,value, times, unit);
    }

    @Override
    public void setnx(String key, Object value) {
        redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    @Override
    public void setnx(String key, Object value, long times, TimeUnit unit) {
        redisTemplate.opsForValue().setIfAbsent(key, value, times, unit);
    }

    @Override
    public <T> T get(String key) {
        return (T)redisTemplate.opsForValue().get(key);
    }

    @Override
    public void del(String key) {
        redisTemplate.opsForValue().getOperations().delete(key);
    }

    @Override
    public void expire(String key, long times, TimeUnit unit) {
        redisTemplate.expire(key, times, unit);
    }

    @Override
    public void persist(String key) {
        redisTemplate.persist(key);
    }

    @Override
    public long ttl(String key) {
        return redisTemplate.getExpire(key);
    }

    @Override
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }
}
